<?php

namespace App\Http\Controllers\Admin\SystemSet\BlackList;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class BlackListModuleController extends BaseController {

    /**
     * @api {post} /system-set/black-list/module 黑名单模块创建
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单模块创建
     * @apiGroup 系统设置
     *
     * @apiParam {string} info                          模块描述 必传
     * @apiParam {number} need_user_level               需要用户等级 必传
     * @apiParam {number} cate                          黑名单类别 必传
     * @apiParam {number} state                         状态 0 关闭  1 启用 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'info' => 'required|string',
                'need_user_level' => 'required|numeric',
                'cate' => 'required|numeric',
                'state' => 'required|numeric|in:0,1',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        $res = app('repo_black_list');

        $data = $res->getModule(["info" => $params['info']]);
        if($data){
            throw new BasicException(10001, '该模块已存在！');
        }

        $params['add_time'] = time();
        $params['add_date_time'] = date("Y-m-d H:i:s");

        $rows = $res->addModule($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /system-set/black-list/module 黑名单模块删除
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单模块删除
     * @apiGroup 系统设置
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_black_list')->delModule($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /system-set/black-list/module 黑名单模块修改
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单模块修改
     * @apiGroup 系统设置
     *
     *
     * @apiParam {number} id
     * @apiParam {string} info                          模块描述 必传
     * @apiParam {number} need_user_level               需要用户等级 必传
     * @apiParam {number} cate                          黑名单类别 必传
     * @apiParam {number} state                         状态 0 关闭  1 启用 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'info' => 'required|string',
                'need_user_level' => 'required|numeric',
                'cate' => 'required|numeric',
                'state' => 'required|numeric|in:0,1',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $res = app('repo_black_list');

        $params = $request->input();
        $id = $params['id'];
        $params['add_time'] = time();
        $params['add_date_time'] = date("Y-m-d H:i:s");

        $rows = $res->saveModule(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /system-set/black-list/module 黑名单模块列表
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单模块列表
     * @apiGroup 系统设置
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
        "code": 200,
        "data": {
            "list": [
                {
                    "id": 26,
                    "info(模块说明)": "test2",
                    "need_user_level(需要用户等级)": 333,
                    "cate(黑名单类别)": 111,
                    "state(状态 0 关闭 1 启用)": 0
                }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
    }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_black_list')->getModuleQueryList($request->input());

        return Response::sendList($list);
    }


}
